<template>
  <div class="InfoBox roundy-20 shadow">
    <div class="title">
      <div
        class="userinfo fontSize-20"
        style="color: rgb(0, 0, 167); font-weight: bold"
      >
        <i>{{ infoList.userName }}</i>
        <img :src="this.infoList.userImg" @click="showModal" />

        <table>
          <tr class="items">
            <td class="itemstitle">注册邮箱:</td>
            <td class="itemsinfo">{{ infoList.userEmail }}</td>
          </tr>
          <tr class="items">
            <td class="itemstitle">注册时间:</td>
            <td class="itemsinfo">{{ infoList.registrationDate }}</td>
          </tr>
          <tr class="items">
            <td class="itemstitle">用户状态:</td>
            <td class="itemsinfo">
              {{ infoList.userState == 0 ? "在线" : "离线" }}
            </td>
          </tr>
          <tr class="items">
            <td class="itemstitle">用户类型:</td>
            <td class="itemsinfo">
              {{ infoList.userType == 1 ? "普通用户" : "管理员" }}
            </td>
          </tr>
        </table>

        <login-button
          @click="showModal"
          class="rightFloat"
          style="margin-left: 35%; margin-top: 30px"
          title="修改个人信息"
        />
        <!-- 修改个人信息 -->
        <div v-if="isModalVisible" class="modal">
          <div class="modal-content">
            <h2>修改个人信息</h2>
            <!-- 修改头像 -->
            <div>
              <img 
              @click="openFileInput"
              class="uploadimg" 
              :src="this.infoList.userImg" 
              alt="上传的图片" 
            />
            <input ref="fileInput" type="file" style="display: none" @change="handleFileUpload" />
            </div>

            <div style="margin-top: 50px;display: flex; justify-content: space-around">
              <button style="background-color: rgb(56, 148, 252);color: white;">确认</button>
              <button @click="hideModal" style="background-color: aliceblue;">关闭</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import LoginButton from "./LoginButton.vue";
export default {
  name: "InfoCenter",
  data() {
    return {
      selectedFile: null,
      //窗口显示
      isModalVisible: false,
      //用户信息
      infoList: {
        userName: "测试用户1",
        password: "xxx",
        userEmail: "xxxxxxx@qq.com",
        userType: 0,
        userState: 1,
        registrationDate: "xxxx年xx月xx日 xx:xx:xx",
        userImg:
          "https://ts1.cn.mm.bing.net/th/id/R-C.0a5dd0a60d9457a9aa9d1b6b990a75e2?rik=NOoaRlopuKZstA&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50051%2f9950.jpg_wh1200.jpg&ehk=tESE5KqtlaOOss8Jb6UhUBX9F8qHCS6aUSnvUC6ut3Q%3d&risl=&pid=ImgRaw&r=0",
      },
    };
  },

  methods: {
    showModal() {
      this.isModalVisible = true;
    },
    hideModal() {
      this.isModalVisible = false;
    },

    openFileInput() {
      this.$refs.fileInput.click();
    },
    handleFileUpload(event) {
      const file = event.target.files[0];
      const reader = new FileReader();

      reader.onload = (e) => {
        this.infoList.userImg = e.target.result;
      };

      reader.readAsDataURL(file);
    }
  
  },
  components: {
    LoginButton,
  },
};
</script>

<style scoped>
.left {
  height: 100vh;
  width: 50%;
  text-align: center;
  line-height: 100vh;
}

.right {
  height: 100vh;
  width: 50%;
  text-align: center;
  line-height: 100vh;
}

.InfoBox {
  height: 600px;
  width: 500px;
  margin: 0px auto;
  /* border: 1px solid rgb(204, 204, 204); */
  background-color: white;
}

.shadow {
  box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.5);
}

.title {
  text-align: center;
  padding: 50px;
}

.text-1 {
  font-size: 20px;
}

.toRegisterBtn {
  color: rgb(94, 94, 194);
  font-size: 18px;
  margin-left: 60px;
}

img {
  width: 150px;
  height: 150px;
  border-radius: 75px;
  margin-left: 30%;
}

.userinfo {
  display: flex;
  flex-direction: column;
  text-align: center;
}

.items {
  display: flex;
  flex-direction: row;
  text-align: left;
}

.itemsinfo {
  margin-left: 30%;
  color: grey;
  font-weight: lighter;
  font-size: 16px;
}
.itemsinfo:hover {
  cursor: pointer;
}
tr {
  margin-top: 30px;
}

.tr td {
  border-bottom: 1px solid;
  margin-top: 10px;
}
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-content {
  width: 350px;
  height: 500px;
  background-color: white;
  padding: 20px;
  border-radius: 4px;
}
.modal-content button {
  width: 120px;
  height: 35px;
  background-color: white;
  border: 1px grey solid;
  border-radius: 10px;
}
.uploadimg{
  font-size: 14px;
  color: grey;
  font-weight: lighter;
  margin-right: 150px;
}
</style>
